var pageSize = 20;
var pageStart = 1;
$(function(){
	loadInfo();
	//删除事件
	$(".ace-thumbnails").on('click','.del',function(){
		var id = $(this).attr("name");
		bootbox.confirm("您确认执行该操作？",function(result){
			if(result){
				delPollPic(id);
			}
		});
	});
});

function delPollPic(id){
	$.ajax({
		url:BASE_PATH+'/polling/del.do',
		type:'get',
		dataType:'json',
		data:{
			id:id
		},
		success:function(data){
			if(data.status=='success'){
				$("a[name='"+id+"']").parents('li').remove();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function loadInfo(){
	$.ajax({
		url:BASE_PATH+'/polling/list.do',
		type:'get',
		dataType : 'json',
		data:{pageSize:pageSize,pageStart:pageStart},
		success : function(data) {
			if(data.errorMsg == 'expire'){
				window.location.href="login.html";
			}else{
				createRows(data);
			}
		}
	});
}

function createRows(data){
	var html = "";
	if(data){
		for(var i = 0; i < data.length; i++){
			html+="<li>"
				+	"<a href='"+BASE_PATH+"/"+data[i].imgUrl+"' data-rel='colorbox'>"
				+		"<img alt='150x150' src='"+BASE_PATH+"/"+data[i].imgUrl+"' />"
				+		"<div class='tags'>"
				+			"<span class='label-holder'>"
				+				"<span class='label label-info arrowed'>"+data[i].title+"</span>"
				+			"</span>"
				+		"</div>"
				+	"</a>"
				+	"<div class='tools tools-top'>"
				+		"<a class='edit' href='addpollpic.html?id="+data[0].id+"'>"
				+			"<i class='icon-pencil'></i>"
				+		"</a>"
				+		"<a class='del' href='#' name='"+data[0].id+"'>"
				+			"<i class='icon-remove red'></i>"
				+		"</a>"
				+	"</div>"
				+"</li>";
		}
	}
	$(".ace-thumbnails").html(html);
	var colorbox_params = {
			reposition:true,
			scalePhotos:true,
			scrolling:false,
			previous:'<i class="icon-arrow-left"></i>',
			next:'<i class="icon-arrow-right"></i>',
			close:'&times;',
			current:'{current} of {total}',
			maxWidth:'100%',
			maxHeight:'100%',
			onOpen:function(){
				document.body.style.overflow = 'hidden';
			},
			onClosed:function(){
				document.body.style.overflow = 'auto';
			},
			onComplete:function(){
				$.colorbox.resize();
			}
		};
	
	$('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
	$("#cboxLoadingGraphic").append("<i class='icon-spinner orange'></i>");
}